//
//  stdVectorAdvance.cpp
//  learnC++
//
//  Created by apple on 2018/6/1.
//  Copyright © 2018年 yunfan.com. All rights reserved.
//

#include "stdVectorAdvance.hpp"

using namespace std;

void printStack(const std::vector<int> &stack)
{
    for (const auto &element : stack)
        std::cout << element << ' ';
    std::cout << "(cap " << stack.capacity() << " length " << stack.size() << ")\n";
}

//resize()分配了内存，capacity（）具备的存储能力大小
void test_vector_advance(){
    
    std::vector<int> array;
    array = { 0, 1, 2, 3, 4 }; // okay, array length = 5
    std::cout << "length: " << array.size() << "  capacity: " << array.capacity() << '\n';
    
    array = { 9, 8, 7 }; // okay, array length is now 3!
    std::cout << "length: " << array.size() << "  capacity: " << array.capacity() << '\n';
    
    //测试堆栈
//    push_back（）推入栈上的元素。
//    back（）返回堆栈顶部元素的值。
//    pop_back（）从堆栈中弹出一个元素。
    vector<int> stack;
    
    stack.reserve(5);//因为调整矢量大小很昂贵，所以我们可以通过reserve（）函数告诉矢量预先分配一定数量的容量
    
    printStack(stack);
    
    stack.push_back(5);
    printStack(stack);
    
    stack.push_back(3);
    printStack(stack);
    
    stack.push_back(2);
    printStack(stack);
    
    std::cout << "top: " << stack.back() << '\n'; // back() returns the last element
    
    stack.pop_back(); // pop_back() back pops an element off the stack
    printStack(stack);
    
    stack.pop_back();
    printStack(stack);
    
    stack.pop_back();
    printStack(stack);
}


